package com.syg.servlet01;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet("/requestParam")
public class _01_ServletRequest extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        /**
         * 1,cookie对象
         */
        //Cookie[] cookies = request.getCookies();
        //System.out.println(cookies[0].getValue());

        /**
         * 2,返回请求中的编码方式
         */
        //String encoding = request.getCharacterEncoding();
        //System.out.println(encoding);

        /**
         * 3,
         */
        //int bodyLength = request.getContentLength();
        //System.out.println(bodyLength);

        /**
         * 4,获取指定头信息
         */
        //String head = request.getHeader("User-Agent");
        //Enumeration heads = request.getHeaders("User-Agent");
        //while (heads.hasMoreElements()){
        //    System.out.println(heads.nextElement());
        //}
        //System.out.println(head);

        /**
         * 5,获取所有头信息
         */
        //Enumeration heads = request.getHeaderNames();
        //while (heads.hasMoreElements()) {
        //    System.out.println(heads.nextElement());
        //}

        /**
         * 6.获取请求中的输入流
         */
        /*InputStream is = request.getInputStream();
        int length = request.getContentLength();
        String pathInfo = request.getPathInfo();
        byte[] bys = new byte[512];
        int len;
        OutputStream os = new FileOutputStream("D:\\Cache\\DelTrash\\1234.xlsx");
        while ((len =is.read(bys))!=-1){
            os.write(bys);
            os.flush();
        }
        System.out.println(pathInfo+length);*/

        /**
         * 7,获取请求方式(POST,GET)
         */
        //String method = request.getMethod();
        //System.out.println(method);

        /**
         * 8,getParameter()、getInputStream()、getReader()三种方式的区别
         * 这三个方法都是从request对象中得到提交的数据
         * 要根据<form>表单提交数据的编码方式选择不同的方法
         */
        //获取指定name的值
        //String parameter = request.getParameter("inputNamesyg");
        //获取所有name字段
        /*Enumeration<String> parameterNames = request.getParameterNames();
        while (parameterNames.hasMoreElements()) {
            System.out.println(parameterNames.nextElement());
        }*/
        //获取复选框的值
        /*String[] strs = request.getParameterValues("有趣111");
        for (String str : strs) {
            System.out.println(str);
        }*/
        //System.out.println(parameter);

        /*String str = "";
        String inputLine = "";
        InputStream is = request.getInputStream();
        InputStreamReader isr = new InputStreamReader(is);
        BufferedReader bd = new BufferedReader(isr);
        while ((inputLine = bd.readLine()) != null) {
            str+=inputLine;
        }
        System.out.println(str);*/

        /**
         * 9,获取数据传输的协议
         */
        /*String protocol = request.getProtocol();
        System.out.println(protocol);*/

        /**
         * 10,获取get方式的请求字符串
         */
        String queryString = request.getQueryString();
        System.out.println(queryString);

        /**
         * 11,URL和URI(包括项目名)区别
         */
        //StringBuffer requestURL = request.getRequestURL();
        //String requestURI = request.getRequestURI();
        //System.out.println(requestURI + requestURL);

        /**
         * 12,获取客户端信息
         */
        /*String remoteUser = request.getRemoteUser();//这个为什么为null
        String remoteAddr = request.getRemoteAddr();
        String remoteHost = request.getRemoteHost();
        int remotePort = request.getRemotePort();
        System.out.println(remoteAddr + "<<-->>" + remoteHost + "<<-->>" + remoteUser + "<<-->>" + remotePort);*/

        /*String serverName = request.getServerName();
        String servletPath = request.getServletPath();
        int serverPort = request.getServerPort();
        System.out.println(serverName + "<<-->>" + servletPath + "<<-->>" + serverPort);*/

    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req,resp);
    }
}
